package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.config.exception.ResultException;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.BasisManageRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.BasisManage;
import com.ktwlsoft.archivemanage.payload.basisManage.BasisSaveRequest;
import com.ktwlsoft.archivemanage.payload.basisManage.BasisUpdateRequest;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.Date;


/**
 * @author JokerMo
 * @version 1.0
 * @date 2020/4/28 14:30
 */
@Component
public class BasisManageService {
    /**
     * 依据管理服务仓储
     */
    private final BasisManageRepositoryService basisManageRepositoryService;


    public BasisManageService(BasisManageRepositoryService basisManageRepositoryService) {
        this.basisManageRepositoryService = basisManageRepositoryService;
    }

    /**
     * 依据分页列表
     *
     * @return Result<BasisManage>
     */
    public Result<BasisManage> findPage(int pn, int pageSize, String basisName) {
        PageInfo<BasisManage> pageInfo = basisManageRepositoryService.findPage(pn, pageSize, basisName);
        return Result.ok(pageInfo);
    }

    /**
     * 获取所有依据
     *
     * @return Result<JSONObject>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<JSONObject> getAll() throws UnsupportedEncodingException {
        return Result.ok(basisManageRepositoryService.getAll());
    }

    /**
     * 依据详情
     *
     * @param id id
     * @return Result<BasisManage>
     */
    public Result<BasisManage> getById(Long id) throws UnsupportedEncodingException {
        JSONObject jsonObject = new JSONObject();
        if (id == null || id <= 0) {
            return Result.body(id, ResultType.CHECK_ERROR).withMessage("id不能为空");
        } else {
            BasisManage basisManage = basisManageRepositoryService.getById(id);
            if (basisManage != null) {
                String content = new String(basisManage.getContent(), "utf-8");
                jsonObject.put("id", basisManage.getId());
                jsonObject.put("basisName", basisManage.getBasisName());
                jsonObject.put("createName", basisManage.getCreateName());
                jsonObject.put("createId", basisManage.getCreateId());
                jsonObject.put("createTime", basisManage.getCreateTime());
                jsonObject.put("content", content);
            }
            return Result.ok(jsonObject);
        }
    }

    /**
     * 依据新增
     *
     * @param request request
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> save(BasisSaveRequest request) throws UnsupportedEncodingException {
        BasisManage isExist = basisManageRepositoryService.selectByBasisName(request.getBasisName());
        if (isExist != null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("依据名重复，请重新输入");
        }
        BasisManage basisManage = new BasisManage();
        BeanUtils.copyProperties(request, basisManage);
        basisManage.setContent(request.getContent().getBytes("utf-8"));
        Account account = UserPrincipal.getCurrentUser();
        basisManage.setCreateName(account.getRealName());
        basisManage.setCreateId(account.getId());
        basisManage.setCreateTime(new Date());
        try {
            basisManageRepositoryService.insert(basisManage);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ResultException(ResultType.SAVING_ERROR.getReason(),ResultType.SAVING_ERROR);
        }
        return Result.ok(basisManage.getId());
    }

    /**
     * 依据更新
     *
     * @param request request
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> update(BasisUpdateRequest request) throws UnsupportedEncodingException {
        BasisManage isExist = basisManageRepositoryService.selectByBasisName(request.getBasisName());
        if (isExist != null && !request.getId().equals(isExist.getId())) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("依据名重复，请重新输入");
        }
        BasisManage basisManage = new BasisManage();
        BeanUtils.copyProperties(request, basisManage);
        basisManage.setContent(request.getContent().getBytes("utf-8"));
        BasisManage findBasisManage = basisManageRepositoryService.getById(basisManage.getId());
        if (findBasisManage == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该记录不存在");
        }
        Account account = UserPrincipal.getCurrentUser();
        basisManage.setUpdateName(account.getRealName());
        basisManage.setUpdateId(account.getId());
        basisManage.setUpdateTime(new Date());
        try {
            basisManageRepositoryService.update(basisManage);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ResultException(ResultType.UPDATE_ERROR.getReason(),ResultType.UPDATE_ERROR);
        }
        return Result.ok(basisManage);
    }

    /**
     * 指南删除
     *
     * @param id id
     * @return Result<?>
     */
    public Result<?> deleteById(Long id) {
        if (id == null || id <= 0) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("id不能为空");
        }
        BasisManage basisManage = basisManageRepositoryService.getById(id);
        if (basisManage == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        try {
            basisManageRepositoryService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.DELETE_ERROR);
        }
        return Result.ok(basisManage.getId());
    }
}
